package com.chinaunicom.structure.proxy.dynamicproxy.cglib;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @author liry
 * @version 1.0
 * @date Created on 2022/8/9 11:05
 * Description:
 */
public class ProxyFactory implements MethodInterceptor {
    private Object target;
    public ProxyFactory(Object o){
        this.target = o;
    }
    
    public Object getProxyInstance(){
        Enhancer enhancer = new Enhancer();
        
        // 设置Enhancer对象的父类
        enhancer.setSuperclass(this.target.getClass());
        
        // 设置回调
        enhancer.setCallback(this);
        return enhancer.create();
    }

    @Override
    public Object intercept(Object o, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
        System.out.println("method interceptor 。。。。。。");
        return method.invoke(this.target, args);
    }
}
